
from itsdangerous import TimedJSONWebSignatureSerializer as Serializer
from meiduo_mall import settings

def generic_openid_token(openid):

    # JWS

    #1. 创建序列化器
    serializer = Serializer(secret_key=settings.SECRET_KEY,expires_in=3600)
    #2. 组织数据
    data = {'openid':openid}
    #3. 加密处理
    token = serializer.dumps(data)
    #4. 加密之后的数据是 bytes类型，转换为字符串
    return token.decode()

    # return openid

from itsdangerous import BadTimeSignature,BadSignature

def check_access_token(token):

    #1. 创建序列化器
    serializer = Serializer(secret_key=settings.SECRET_KEY, expires_in=3600)
    #2. 对数据进行解密
    try:
        data = serializer.loads(token)
        # data = {'openid':openid}
    # except BadTimeSignature:
    except BadSignature:
        return None


    openid = data.get('openid')

    return openid
